package hyo.hun.lifemanager;

import hyo.hun.lifemanager.common.DBSQLite;
import hyo.hun.lifemanager.common.SuperActivity;
import hyo.hun.lifemanager.connect.CalendarConnect;
import hyo.hun.lifemanager.month.MonthActivity;
import hyo.hun.lifemanager.today.TodayActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntroActivity extends SuperActivity {

	private DBSQLite dbSql;
	
	Button btn_today;
	Button btn_month;
	
	private Handler mHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			new InitBackground().execute(0);
		}; 
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro_activity);
		
		dbSql = DBSQLite.getInstance(superContext);
		
		mHandler.sendEmptyMessageDelayed(0, 1000);
	}
	
	int startType = 0;
	private void goMain(int type) 
	{
		Intent intent = null;
		
		switch (startType) 
		{
			case 0:
				intent = new Intent(IntroActivity.this, TodayActivity.class);
				break;
			case 1:
				intent = new Intent(IntroActivity.this, MonthActivity.class);
				break;
			default:
				break;
		}
	
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
		finish();
		//overridePendingTransition(R.anim.right_in, R.anim.left_out);
	}
	
	private class InitBackground extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(IntroActivity.this);
			dialog.setCancelable(false);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0)
			{
				DBInit init = new DBInit(superContext);
				init.initDropTable(superContext);
				init.setTable(superContext);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (dialog != null)
			{
				dialog.dismiss();
				dialog = null;
			}
			
			btn_today = (Button)findViewById(R.id.intro_activity_button_today);
			btn_month = (Button)findViewById(R.id.intro_activity_button_month);
			
			btn_today.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startType = 0;
					goMain(startType);
				}
			});
			
			btn_month.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startType = 1;
					goMain(startType);
				}
			});
			
			CalendarConnect.getCalendarConnect().startConnect(superContext, IntroActivity.this);
			//goMain(startType);
		}
	}

}
